<?php

/**
 * @desc: Plugin for config application multi languages
 * 
 * @author: Tuyendn<tuyendn@vnext.vn>
 * @since: 2012/02/21
 * */
class Vlib_Plugin_Language extends Zend_Controller_Plugin_Abstract {

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $_request) {
        if (!Zend_Registry::isRegistered('Zend_Translate')) {
            $_lang = $_request->getParam('lang');
            $_moduleName = $_request->getModuleName();
            $_appConf = Vlib_Config::getConfig();
            if (!$_lang) {
                $_lang = $_appConf['web']['language'] ? $_appConf['web']['language'] : 'ja';
            }
            // get application language file
            $_appLang = APPLICATION_PATH . DS . 'languages' . DS . 'lang-' . $_lang . '.php';
            $_moduleDir = Vlib_Module_Loader::getIntance()->getModuleDir();
            $_moduleLang = $_moduleDir . DS . $_moduleName . DS . 'languages' . DS . 'lang-' . $_lang . '.php';

            // throw exception if application language file not found
            if (!file_exists($_appLang)) {
                throw new Exception('application language file not found');
            }

            // throw exception if module language file not found
            if (!file_exists($_moduleLang)) {
                throw new Exception('module language file not found');
            }

            $_translate = new Zend_Translate('Array', $_appLang, $_lang);
            $_translate->addTranslation($_moduleLang, $_lang);
            $_translate->setLocale($_lang);
            Zend_Registry::set('Zend_Translate', $_translate);
        }
    }

}